package com.zcsy.seller.web.order;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zcsy.commons.constants.Constants;
import com.zcsy.commons.model.RequestConstants;
import com.zcsy.commons.model.RequestJson;
import com.zcsy.manage.model.base.CellInfo;
import com.zcsy.manage.model.base.ShopInfo;
import com.zcsy.manage.model.mall.GoodsInfo;
import com.zcsy.manage.model.mall.OrderDetailInfoView;
import com.zcsy.manage.model.mall.OrderInfo;
import com.zcsy.manage.model.system.User;
import com.zcsy.manage.persistence.mall.OrderDetailInfoDao;
import com.zcsy.manage.service.mall.IGoodsInfoService;
import com.zcsy.manage.service.mall.IOrderInfoService;

/**
 * @Title: OrderController.java
 * @Package： com.zcsy.seller.web.order 
 * @Description: 商户web订单控制层
 *
 * @author caiyinli 13277030520@163.com
 * @Copyright: 2015 武汉中财数元信息技术有限公司 All rights reserved.
 * @date 2016年6月1日
 * @version 1.0
 * @see JDK1.7.0_75
 * @since
 */
@Controller
@RequestMapping(value = "/order")
public class OrderController {

	@Autowired
	private IOrderInfoService orderInfoService;
	
	@Autowired
	private OrderDetailInfoDao orderDetailInfoDao;
	
    @RequestMapping(value="orderList" , method = RequestMethod.GET)
    public String orderList(Model model, Integer pNo,Integer state,String orderNumber,String contactor,
    		String phone,String beginTime,String endTime,Integer stateType,HttpServletRequest req){
    	
	    if(pNo == null){
            pNo = Constants.PAGE_NO;
        }
	    
	    /*if(state == null && stateType == null){
	    	state = Constants.ORDER_ALL;
	    }*/
	    
        //此处添加PageHelper将会走拦截器，进行分页，分页方法只针对紧跟着PageHelper的查询方法有效
        PageHelper.startPage(pNo, Constants.PAGE_SIZE);
        
        OrderInfo order = new OrderInfo();
        
        order.setState(state);
        order.setOrderNumber(orderNumber);
        order.setContactor(contactor);
        order.setContactorPhone(phone);
        
        if(StringUtils.isNotEmpty(beginTime)){
        	order.setBeginTime(beginTime);
        }
        
        if (StringUtils.isNotEmpty(endTime)) {
			order.setEndTime(endTime);
		}
        if(stateType != null ){
        	order.setStateType(stateType);
        }
        
        ShopInfo shopInfo = (ShopInfo)req.getSession().getAttribute(RequestConstants.SHOP_ID);
        order.setShopId(shopInfo.getId());
        
        List<OrderInfo> orders = orderInfoService.selectSellerOrderListByKeyword(order);
        PageInfo<OrderInfo> info = new PageInfo<OrderInfo>(orders);
        
		model.addAttribute("order", info);
		model.addAttribute("currentPage", pNo);
		model.addAttribute("orderNumber", orderNumber);
		model.addAttribute("contactor", contactor);
		model.addAttribute("phone", phone);
		model.addAttribute("state", state);
		model.addAttribute("beginTime", beginTime);
		model.addAttribute("endTime", endTime);
		List<OrderDetailInfoView> orderDetails = new ArrayList<OrderDetailInfoView>();
		for (OrderInfo orderInfo : orders) {
			orderDetails = orderDetailInfoDao.getByOrderId(orderInfo.getId());
			orderInfo.setOrderDetails(orderDetails);
		}
		
        return "order/orderList";
    }
    
	@RequestMapping(value="updateOrderState" , method = RequestMethod.GET)
	@ResponseBody
    public RequestJson updateOrderState( String id, int state, HttpServletRequest req){
        RequestJson result = new RequestJson();
        User user = (User)req.getSession().getAttribute(RequestConstants.USER_SESSION_ID);
        try {
          int flag = Constants.SQL_RESULT_ERROR;
          OrderInfo orderInfo =  orderInfoService.selectByPrimaryId(id);
          
          if(orderInfo.getState() == Constants.ORDER_UNSEND) {
	          if(orderInfo.getSenderAccount() != null) {
	        	  
	        	  flag = orderInfoService.updateOrderStateByMap(id, state, user.getLoginname());
	        	  
	        	  if(flag==Constants.SQL_RESULT_SUCCESS) {
	        		  result.setSuccess(true);
	        	  } else {
	        		  result.setSuccess(false);
	        		  result.setMsg("操作失败");
	        	  }
	        	  
	          } else {
	        	  result.setSuccess(false);
	        	  result.setMsg("无配送人员，无法发货！");
	          }
	          
          } else {
        	  flag = orderInfoService.updateOrderStateByMap(id, state, user.getLoginname());
        	  
        	  if(flag==Constants.SQL_RESULT_SUCCESS) {
        		  result.setSuccess(true);
        	  } else {
        		  result.setSuccess(false);
        		  result.setMsg("操作失败");
        	  }
          }
          
        } catch (Exception e) {
            e.printStackTrace();
            result.setMsg("系统故障");
            result.setSuccess(false);
        }
        return result;
    }
	
	@RequestMapping(value = "/orderDetail", method = RequestMethod.GET)
	public String orderDetail(Model model,String orderId,HttpServletRequest req){
		OrderInfo orderInfo = orderInfoService.selectOrderByPrimaryKey(orderId);
		List<OrderDetailInfoView> orderDetailList = orderDetailInfoDao.getByOrderId(orderInfo.getId());
		orderInfo.setOrderDetails(orderDetailList);
		model.addAttribute("orderDetail",orderInfo);
		//model.addAttribute("orderDetailInfoList",orderDetailInfoList);
		
		return "order/orderDetail";
	}
}
